package com.hsytech.dijiebao.photos.activities;

import java.io.File;
import java.util.ArrayList;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.photos.fragments.MultiImageSelectorFragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 澶氬浘閫夋嫨
 */
public class MultiImageSelectorActivity extends FragmentActivity implements MultiImageSelectorFragment.Callback {
	/**
	 * 鏈�澶у浘鐗囬�夋嫨娆℃暟锛宨nt绫诲瀷锛岄粯璁�1
	 */
	public static final String EXTRA_SELECT_COUNT = "1";
	/**
	 * 鍥剧墖閫夋嫨妯�?�紡锛岄粯璁ゅ閫�
	 */
	public static final String EXTRA_SELECT_MODE = "select_count_mode";
	/**
	 * 鏄惁鏄剧ず鐩告�?锛岄粯璁ゆ樉�?�?
	 */
	public static final String EXTRA_SHOW_CAMERA = "show_camera";
	/**
	 * 閫夋嫨缁撴灉锛岃繑鍥炰负 ArrayList&lt;String&gt; 鍥剧墖璺緞闆嗗�?
	 */
	public static final String EXTRA_RESULT = "select_result";
	/**
	 * 榛樿閫夋嫨闆�
	 */
	public static final String EXTRA_DEFAULT_SELECTED_LIST = "default_list";
	/**
	 * 鍗曢��
	 */
	public static final int MODE_SINGLE = 0;
	/**
	 * 澶氶��
	 */
	public static final int MODE_MULTI = 1;
	private ImageView iv_zheng;
	private ArrayList<String> resultList = new ArrayList<String>();
	private Button mSubmitButton;
	private int mDefaultCount;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_default);

		Intent intent = getIntent();
		mDefaultCount = intent.getIntExtra(EXTRA_SELECT_COUNT, 8);
		int mode = intent.getIntExtra(EXTRA_SELECT_MODE, MODE_MULTI);
		boolean isShow = intent.getBooleanExtra(EXTRA_SHOW_CAMERA, false);
		if (mode == MODE_MULTI && intent.hasExtra(EXTRA_DEFAULT_SELECTED_LIST)) {
			resultList = intent.getStringArrayListExtra(EXTRA_DEFAULT_SELECTED_LIST);
		}

		Bundle bundle = new Bundle();
		bundle.putInt(MultiImageSelectorFragment.EXTRA_SELECT_COUNT, mDefaultCount);
		bundle.putInt(MultiImageSelectorFragment.EXTRA_SELECT_MODE, mode);
		bundle.putBoolean(MultiImageSelectorFragment.EXTRA_SHOW_CAMERA, isShow);
		bundle.putStringArrayList(MultiImageSelectorFragment.EXTRA_DEFAULT_SELECTED_LIST, resultList);

		getSupportFragmentManager().beginTransaction()
				.add(R.id.image_grid, Fragment.instantiate(this, MultiImageSelectorFragment.class.getName(), bundle))
				.commit();
		// 杩斿洖鎸夐挳
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});

		// 瀹屾垚鎸夐挳
		mSubmitButton = (Button) findViewById(R.id.commit);
		if (resultList == null || resultList.size() <= 0) {
			mSubmitButton.setText("完成");
			mSubmitButton.setEnabled(false);
		} else {
			mSubmitButton.setText("完成(" + resultList.size() + "/" + mDefaultCount + ")");
			mSubmitButton.setEnabled(true);
		}
		mSubmitButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				//
				// Toast.makeText(MultiImageSelectorActivity.this,
				// resultList.get(0), Toast.LENGTH_SHORT).show();
				if (resultList != null && resultList.size() > 0) {
					Log.e("mylog", "resultList!=null");
					// 杩斿洖宸查�夋嫨鐨勫浘鐗囨暟鎹�
					Intent data1 = new Intent();
					// 鐓х墖
					data1.putStringArrayListExtra(EXTRA_RESULT, resultList);

					// data1.putExtra("null","a");
					setResult(Activity.RESULT_OK, data1);

					finish();
				}
			}
		});
	}

	@Override
	public void onSingleImageSelected(String path) {
		Intent data = new Intent();
		resultList.add(path);
		data.putStringArrayListExtra(EXTRA_RESULT, resultList);
		setResult(RESULT_OK, data);
		finish();
	}

	@Override
	public void onImageSelected(String path) {
		if (!resultList.contains(path)) {
			resultList.add(path);
		}
		// 鏈夊浘鐗囦箣鍚庯紝鏀瑰彉鎸夐挳鐘舵��?
		if (resultList.size() > 0) {
			mSubmitButton.setText("完成(" + resultList.size() + "/" + mDefaultCount + ")");
			if (!mSubmitButton.isEnabled()) {
				mSubmitButton.setEnabled(true);
			}
		}
	}

	@Override
	public void onImageUnselected(String path) {
		if (resultList.contains(path)) {
			resultList.remove(path);
			mSubmitButton.setText("完成(" + resultList.size() + "/" + mDefaultCount + ")");
		} else {
			mSubmitButton.setText("完成(" + resultList.size() + "/" + mDefaultCount + ")");
		}
		// 褰撲负閫夋嫨鍥剧墖鏃跺�欑殑鐘舵��
		if (resultList.size() == 0) {
			mSubmitButton.setText("完成");
			mSubmitButton.setEnabled(false);
		}
	}

	@Override
	public void onCameraShot(File imageFile) {
		if (imageFile != null) {
			Intent data = new Intent();
			resultList.add(imageFile.getAbsolutePath());
			data.putStringArrayListExtra(EXTRA_RESULT, resultList);
			setResult(RESULT_OK, data);
			finish();
		}

	}
}
